import 'dart:math';

abstract class ICompress {
  void onSuccess(String srcPath, String resultPath);

  void onError(String error);

  void start();
}

class CompressImpl implements ICompress {
  Function(String, String)? _onSuccess;
  Function(String)? _onError;
  Function()? _start;

  void OnSuccess(Function(String, String) method) {
    _onSuccess = method;
  }

  void OnError(Function(String) method) {
    _onError = method;
  }

  void OnStart(Function() method) {
    _start = method;
  }

  @override
  void onSuccess(String srcPath, String resultPath) {
    _onSuccess?.call(srcPath, resultPath);
  }

  @override
  void onError(String error) {
    _onError?.call(error);
  }

  @override
  void start() {
    _start?.call();
  }
}

typedef ComprerssSuccess = void Function(String srcPath, String resultPath);

class NetTest {
  CompressImpl? mCallback;

  void start({required ComprerssSuccess onSuccess, required Function(String error) onError}) {
    mCallback?.start();
    Future.delayed(Duration(milliseconds: 500), () {
      int result = Random().nextInt(10);
      if (result > 5) {
        onSuccess("123", "456");
        mCallback?.onSuccess("123", "456");
      } else {
        onError("错误的消息");
        mCallback?.onError("错误的消息");
      }
    });
  }

  void setCompress(CompressImpl callback) {
    mCallback = callback;
  }
}

extension CompressDsl on NetTest {
  void setCompressDsl(void Function(CompressImpl) init) {
    CompressImpl listener = CompressImpl();
    init.call(listener);
    setCompress(listener);
  }
}
